package top.heue.utils.container

import android.content.Intent
import androidx.fragment.app.Fragment

data class ContainConfig(
    val id: Int,
    var container: Class<*>,
    var tag: String = id.toString(),
    var isNewTask: Boolean = false,
    var isFit: Boolean = true,
    var fragmentClass: Class<*>,
    /** 生命周期相关回调*/
    var callbackClass: Class<out ContainCallback>? = null,
    /** intent相关，如果设定就以此intent打开*/
    var intent: Intent? = null,
    /** 传递fragment任意构建参数*/
    var params: List<Any>,
    var callbackParams: List<Any>? = null
) {
    /** 每一次获取都会构建新的实例*/
    fun getFragment(): Fragment = run {
        val types = mutableListOf<Class<*>>()
        val values = mutableListOf<Any>()
        params.forEach {
            types.add(it::class.java)
            values.add(it)
        }
        fragmentClass.getConstructor(*types.toTypedArray())
            .newInstance(*values.toTypedArray()) as Fragment
    }

    /** 每一次获取都会构建新的实例*/
    fun getCallback(activity: ContainActivity): ContainCallback? {
        if (callbackClass == null) return null
        return callbackClass!!
            .getConstructor(
                ContainActivity::class.java,
                ContainConfig::class.java,
                List::class.java
            )
            .newInstance(activity, this, callbackParams)
    }
}